import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    token:'',
    //初始化时通过反序列获取
    // userInfo:JSON.parse(sessionStorage.getItem("userInfo")),
    userInfo:JSON.parse(localStorage.getItem("userInfo")),
  },
  mutations: {
    //修改-set
    SET_TOKEN:(state,token)=>{ //token类似传入的参数
      state.token = token;
      //存入浏览器--浏览器关闭时不会丢失--
      localStorage.setItem('token',token);
    },
    SET_USERINFO:(state,userInfo)=>{ //userInfo类似传入的参数
      state.userInfo = userInfo;
      //存入浏览器--浏览器关闭丢失再通过token获取-只能存字符串--存对象必须要序列化
      // sessionStorage.setItem('userInfo',JSON.stringify(userInfo));
      localStorage.setItem('userInfo',JSON.stringify(userInfo))
    },
    /*上面两个的值都删除*/
    REMOVE_INFO:(state)=>{
      //统统变为空
      state.token = '';
      state.userInfo={};
      localStorage.setItem("token",'');
      // sessionStorage.setItem("userInfo",JSON.stringify(''))
      localStorage.setItem("userInfo",JSON.stringify(''))
    }
  },
  getters:{
    //获取--get
   getUser: state =>{
     return state.userInfo
   }
  },
  actions: {
  },
  modules: {
  }
})
